import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_checking/checking/radar_painter.dart';

import 'checking_widget.dart';

class CheckingSign extends StatefulWidget {
  @override
  _CheckingSignState createState() => _CheckingSignState();
}

class _CheckingSignState extends State<CheckingSign> with TickerProviderStateMixin {
  // 地址刷新
  late AnimationController controller;

  // 签到雷达
  late AnimationController radarController;

  // 签到雷达动画
  late Animation<double> animation;

  // 签到按钮时间
  late String time;

  // 是否显示签到雷达动画
  bool isShowRadar = false;
  String address = "浙江省杭州市西湖区龙井路1号";
  List<String> addresses = [
    "湖北省武汉市武昌区蛇山西山坡特1号",
    "上海市浦东新区世纪大道1号",
    "北京市东城区长安街",
    "浙江省杭州市西湖区龙井路1号",
    "广东省广州市白云区同和街道广园中路801号",
    "深圳市南山区深南大道9037号"
  ];

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(duration: const Duration(seconds: 1), vsync: this);

    radarController =
        AnimationController(duration: const Duration(seconds: 1), vsync: this);
    radarController.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        isShowRadar = false;
        CheckingWidget().showSignDialog(context,
            CheckingWidget().getSignDialogContent(5), onTapSignConfirm);
      }
    });

    animation = Tween(begin: .0, end: pi * 2).animate(radarController);

    time = CheckingWidget().getCurrentTime(DateTime.now());
    // 当前时间
    Timer.periodic(Duration(seconds: 1), (timer) async {
      time = CheckingWidget().getCurrentTime(DateTime.now());
      print("time:$time");
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          CheckingWidget().buildSignContainer(
              context,
              // 考勤地址
              CheckingWidget().buildSignLocation(
                  address,
                  RotationTransition(
                    turns: controller,
                    child: CheckingWidget()
                        .buildSignRefreshButton(onTapRefreshLocation),
                  )),
              // 考勤按钮
              CheckingWidget().buildSignButton(
                  DateTime.now().hour,
                  time,
                  1,
                  Visibility(
                    visible: isShowRadar,
                    child: AnimatedBuilder(
                        animation: animation,
                        builder: (context, child) {
                          return Container(
                            height: 144.0,
                            width: 144.0,
                            child: CustomPaint(
                              painter: RadarPainter(animation.value),
                            ),
                          );
                        }),
                  ),
                  onTapSign))
        ],
      ),
    );
  }

  /// 刷新地址-动画开始时重新开始获取当前位置信息
  onTapRefreshLocation() {
    print("onTapRefreshLocation");
    if (controller.status == AnimationStatus.completed) {
      controller.reset();
      controller.forward();
    } else {
      controller.forward();
    }
    address = addresses[Random().nextInt(addresses.length)];
    // print("index:${addresses.length.nextInt(100)}");
    setState(() {

    });
  }

  /// 点击签到，若上午时间晚于9点，则为迟到，下午早于6点，则为早退，若当天无签到，则一直为迟到状态
  /// 点击按钮后执行动画，并请求签到接口，根据接口状态，展示不同的弹框内容
  onTapSign() {
    print("onTapSign");
    isShowRadar = true;
    radarController.reset();
    radarController.forward();
  }

  /// 签到完成后，展示不同的提示语，点击弹窗中确认按钮弹窗消失
  onTapSignConfirm() {
    print("onTapSignConfirm");
    Navigator.pop(context);
  }

  @override
  void dispose() {
    radarController.dispose();
    controller.dispose();
    super.dispose();
  }
}
